// distribution/consumer/consumer.js
const api = require("../../request/api")
const request = require("../../request/request")
const util = require("../../utils/util")
const App = getApp();

Page({

    /**
     * 页面的初始数据
     */
    data: {
        barHeight: App.globalData.paddingTop,
        styledata: {
            "bg_color": "white",
            "color": "#000",
            "leftflag": true,
            // "homeflag": true,
            "name": "分销客户"
        },
        tabPanelstyle: 'display:flex;justify-content:center;align-items:center;',

        uid: '',
        loading_text: "暂无数据",

        // 每次加载记录条数
        limit: 10,
        // 是否为刷新列表
        ispullrefresh: false,


        all_list: [],
        today_list: [],

        allpage: 0,
        todaypage: 0,

        noMoreAll: false,
        noMoreToday: false,

        todaypull: false,
        allpull: false,


        // 当前tap索引
        currentindex: 0
    },

    // tab切换事件
    async onTabsChange(event) {
        // console.log(`Change tab, tab-panel value is ${event.detail.value}.`);
        let index = event.detail.value
        this.setData({
            currentindex: index
        })
        await this.getConsumerList(`${index}`)
    },

    // 按类型请求分销客户填入对应数组
    async getConsumerList(type = '0') {

        // 针对上拉加载 type为事件对象的情况
        let typeNum = Number(type)
        if (isNaN(typeNum)) {
            type = `${this.data.currentindex}`
        }

        // 上拉加载 whichlist变量需要加到data中保存
        let whichList // 选择对应的数组
        let pagenum
        let noMoreFlag
        let typenum

        switch (type) {
            case '0':
                whichList = 'today_list';
                pagenum = ++this.data.todaypage
                noMoreFlag = 'noMoreToday'
                typenum = 1
                this.setData({ todaypage: pagenum })
                break;
            case '1':
                whichList = 'all_list';
                pagenum = ++this.data.allpage
                noMoreFlag = 'noMoreAll'
                typenum = 0
                this.setData({ allpage: pagenum })
                break;
            default:
                break;
        }

        // 判断是否已经到底
        if (this.data[noMoreFlag]) {
            // util.hideLoading()
            return
        }

        util.showLoading('正在加载')

        let params = {
            user_id: this.data.uid,
            type: typenum,
            page: pagenum,
            limit: this.data.limit
        }

        // 如果为下拉刷新，重新开辟新数组填充数据
        let ispullrefresh = this.data.ispullrefresh

        let list = ispullrefresh ? [] : this.data[whichList]
        let res = await request.post(api.queryMymember, params)


        if (res.length == 0) {
            this.setData({
                [noMoreFlag]: true
            })
            // 如果是下拉刷新 需要清空原列表
            if (ispullrefresh) {
                this.setData({
                    [whichList]: [],
                })
            }
            util.hideLoading()
            return true
        }
        let newlist = list.concat(res)

        this.setData({
            [whichList]: newlist,
        })
        util.hideLoading()
        return true
    },

    // 今日分账 下拉刷新
    pullRefresh1(e) {
        this.setData({
            todaypage: 0,
            noMoreToday: false,
            ispullrefresh: true,
            todaypull: true,
        })

        setTimeout(async () => {
            let flag = await this.getConsumerList("0")
            if (flag) {
                this.setData({
                    todaypull: false,
                    ispullrefresh: false
                })
            }
        }, 1500);
    },

    // 累计分账 下拉刷新
    pullRefresh2(e) {
        this.setData({
            allpage: 0,
            noMoreAll: false,
            ispullrefresh: true,
            allpull: true,
        })

        setTimeout(async () => {
            let flag = await this.getConsumerList("1")
            if (flag) {
                this.setData({
                    allpull: false,
                    ispullrefresh: false
                })
            }
        }, 1500);
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {
        const that = this
        wx.getSystemInfo({
            success(res) {
                // console.log('机型信息', res.screenHeight);
                that.setData({
                    tabBoxHeight: res.screenHeight - App.globalData.paddingTop,
                    innerBoxHeight: res.screenHeight - App.globalData.paddingTop - 48
                })
            }
        })

        let { type } = options
        this.setData({
            uid: App.globalData.uid,
            type: options.type,
            currentindex: type
        })

        this.getConsumerList(type)
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom() {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage() {

    }
})